﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace DMU.Gui.RPT
{
    public partial class ucRPTSinhVienTheoMon : DevExpress.XtraEditors.XtraUserControl
    {
        public ucRPTSinhVienTheoMon()
        {
            InitializeComponent();
        }
        Service sv=new Service();
        help hp = new help();

        private DataTable dtSV;
        private void lkpLop_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (lkpLop.Text.Trim() != "")
                {
                    DataTable dt= sv.getDatatable("dmu_proPublicFindLop", new SqlParameter("name", lkpLop.Text + "%"));
                    lkpLop.Properties.DataSource = dt;
                    lkpLop.Properties.ValueMember = "MaLop";
                    lkpLop.Properties.DisplayMember = "TenLop";
                }

            }
        }

        private void lkpMonHoc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (lkpMonHoc.Text.Trim() != "")
                {
                    DataTable dt = sv.getDatatable("dmu_proPublicFindMonHoc", new SqlParameter("name", lkpMonHoc.Text + "%"));
                    lkpMonHoc.Properties.DataSource = dt;
                    lkpMonHoc.Properties.ValueMember = "MaMH";
                    lkpMonHoc.Properties.DisplayMember = "TenMH";
                }
            }
        }

        private void lkpLop_EditValueChanged(object sender, EventArgs e)
        {
            if (lkpLop.EditValue != null)
            {
                DataTable dt = sv.getDatatable("dmu_proSFindSVToLop", new SqlParameter("MaLop0", lkpLop.EditValue.ToString().Trim()));
                gLkpSinhVien.Properties.DataSource=dt;
                gLkpSinhVien.Properties.ValueMember = "MSSV";
                gLkpSinhVien.Properties.DisplayMember = "HoTen";
                
            }
        }

        private void gLkpSinhVien_EditValueChanged(object sender, EventArgs e)
        {
            if (gLkpSinhVien.EditValue != null)
            {
                //lkpMonHoc.Properties.DataSource = sv.getDatatable("dmu_proRPLoadMonHoc", new SqlParameter("MSSV0", gLkpSinhVien.EditValue.ToString().Trim()));
                lkpMonHoc.Properties.ValueMember = "MaMH";
                lkpMonHoc.Properties.DisplayMember = "TenMH";
                MessageBox.Show(gLkpSinhVien.EditValue.ToString().Trim());
            }
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            if (gLkpSinhVien.EditValue == null)
            {
                gLkpSinhVien.Focus();
                return;
            }
            if (lkpMonHoc.EditValue == null)
            {
                lkpMonHoc.Focus();
                return;
            }
            SqlParameter[] pr= new SqlParameter[2];
            pr[0]=new SqlParameter("MSSV0",gLkpSinhVien.EditValue.ToString());
            pr[1]=new SqlParameter("MaMH0",lkpMonHoc.EditValue.ToString());

            dtSV = sv.getDatatable("dmu_proRPSinhVienTheoMon", pr);
            gridControl1.DataSource = dtSV;
        }

        private void gridView4_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
                e.Value = e.ListSourceRowIndex + 1;
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            if (dtSV.Rows.Count > 0)
            {
                rpSinhVienTheoMon rpt = new rpSinhVienTheoMon();
                rpt.bindingSource1.DataSource = dtSV;
                rpt.lblSinhVien.Text = "Tên SV: "+gLkpSinhVien.EditValue.ToString()+" - "+ gLkpSinhVien.Text;
                rpt.lblMonHoc.Text = "Môn: " + lkpMonHoc.Text;

                float Vang = 0, sTietVang = 0, TongTiet = 0;
                float tiLe = 0;
                foreach (DataRow row in dtSV.Rows)
                {
                    TongTiet += int.Parse(row["SoTiet"].ToString());
                    if (row["VangTre"].ToString() == "Vắng")
                    {
                        Vang++;
                        sTietVang += int.Parse(row["SoTiet"].ToString());
                    }
                }

                tiLe=(TongTiet - Vang) / TongTiet * 100;
                rpt.lblDiHoc.Text += TongTiet - Vang;
                rpt.lblVang.Text += sTietVang;
                rpt.lblDuLop.Text += Math.Round(tiLe,2);
                rpt.ShowRibbonPreviewDialog();
            }
            else
                hp.showMes("Dữ liệu rỗng !! Không thể in ", -1);
        }
    }
}
